//
//  TitlePageView.m
//  仿斗鱼主页OC版
//
//  Created by baohuai on 16/10/15.
//  Copyright © 2016年 baohuai. All rights reserved.
//

#import "TitlePageView.h"
#define scrollLineH 2
#define bottomLineH 0.1
@interface TitlePageView ()
@property (nonatomic, strong)NSArray * titles; // 标题数组
@property (nonatomic, strong)UIScrollView * scrollView; // 滚动视图
@property (nonatomic, strong)UILabel * scrollLine; // 滚动的线
@property (nonatomic, strong)NSMutableArray * labelArray; // 存放标题label
@end
@implementation TitlePageView

// MARK -- 重写构造函
-(instancetype)initWithFrame:(CGRect)frame WithTitleArray:(NSArray*)titles {
    self = [super initWithFrame:frame];
    if (self) {
        self.titles = titles;
        [self setupUI];
    }
    return self;
}

-(void)setupUI {
    //1.设置标题
    [self setupScrollViewAndTitlesView];
    
    //2.设置底部线 和  滚动的线
    [self setupBottomLineAndScrollLine];
}

-(void)setupScrollViewAndTitlesView {
    
    float width = kScreenW/self.titles.count;
    float height = self.bounds.size.height - scrollLineH;
    for (int i=0; i<self.titles.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(width*i, 0, width,height)];
        label.text = self.titles[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont systemFontOfSize:16];
        [self.scrollView addSubview:label];
        [self.labelArray addObject:label];
    }
}

-(void)setupBottomLineAndScrollLine {
    //1.设置线
    UILabel *bottomLine = [[UILabel alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-bottomLineH, kScreenW, bottomLineH)];
    bottomLine.backgroundColor = [UIColor darkGrayColor];
    [self addSubview:bottomLine];
    
    //2.设置滚动的线
    UILabel *firstLb  = self.labelArray.firstObject;
    float scrollLineY = firstLb.frame.size.height;
    float width = firstLb.frame.size.width;
    self.scrollLine = [[UILabel alloc] initWithFrame:CGRectMake(0, scrollLineY, width, scrollLineH)];
    self.scrollLine.backgroundColor = [UIColor orangeColor];
    [self.scrollView addSubview:self.scrollLine];
}
#pragma mark -lazyload
-(NSArray *)titles {
    if (!_titles) {
        _titles = [NSArray array];
    }
    return _titles;
}
-(NSMutableArray *)labelArray {
    if (!_labelArray) {
        _labelArray = [NSMutableArray array];
    }
    return _labelArray;
}
-(UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, self.bounds.size.height)];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.scrollsToTop = NO;
        _scrollView.bounces = NO;
        [self addSubview:_scrollView];
    }
    return _scrollView;
}
@end
